import { ArchiveComments } from './archiveComment';
let _ArchiveComments = ArchiveComments;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import ArchiveCommentMock from './mdp/arc/archiveCommentMock';
 * ArchiveCommentMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mdp/arc/archiveComment/listKey').reply(config => {
      let params = config.params;
      let mockArchiveComments = _ArchiveComments.filter(archiveComment => {
      	if(params.id||params.id==''){ if( archiveComment.id.indexOf(params.id)>=0) return true;}
      	if(params.userid||params.userid==''){ if( archiveComment.userid.indexOf(params.userid)>=0) return true;}
      	if(params.username||params.username==''){ if( archiveComment.username.indexOf(params.username)>=0) return true;}
      	if(params.star||params.star==''){ if( archiveComment.star.indexOf(params.star)>=0) return true;}
      	if(params.context||params.context==''){ if( archiveComment.context.indexOf(params.context)>=0) return true;}
      	if(params.createDate||params.createDate==''){ if( archiveComment.createDate.indexOf(params.createDate)>=0) return true;}
      	if(params.archiveId||params.archiveId==''){ if( archiveComment.archiveId.indexOf(params.archiveId)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockArchiveComments
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mdp/arc/archiveComment/list').reply(config => {
      let params = config.params;
      let mockArchiveComments = _ArchiveComments.filter(archiveComment => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(archiveComment.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.userid||params.userid==''){if(archiveComment.userid.indexOf(params.userid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.username||params.username==''){if(archiveComment.username.indexOf(params.username)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.star||params.star==''){if(archiveComment.star.indexOf(params.star)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.context||params.context==''){if(archiveComment.context.indexOf(params.context)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.createDate||params.createDate==''){if(archiveComment.createDate.indexOf(params.createDate)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.archiveId||params.archiveId==''){if(archiveComment.archiveId.indexOf(params.archiveId)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockArchiveComments
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockArchiveComments.length;
      mockArchiveComments = mockArchiveComments.filter((archiveComment, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockArchiveComments,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mdp/arc/archiveComment/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _ArchiveComments = _ArchiveComments.filter(archiveComment => archiveComment.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mdp/arc/archiveComment/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _ArchiveComments = _ArchiveComments.filter(archiveComment => !params.includes(archiveComment.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mdp/arc/archiveComment/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,userid,username,star,context,createDate,archiveId } = params;
      _ArchiveComments.some(archiveComment => {
        if (archiveComment.id === id) {
			archiveComment.id = id;
			archiveComment.userid = userid;
			archiveComment.username = username;
			archiveComment.star = star;
			archiveComment.context = context;
			archiveComment.createDate = createDate;
			archiveComment.archiveId = archiveId
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mdp/arc/archiveComment/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,userid,username,star,context,createDate,archiveId } = params;
      _ArchiveComments.push({
		id:id,
		userid:userid,
		username:username,
		star:star,
		context:context,
		createDate:createDate,
		archiveId:archiveId
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};